package cn.wen.yaling.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.wen.yaling.commons.enums.LimitNum;
import cn.wen.yaling.commons.vo.*;
import cn.wen.yaling.xo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.TagEntity;
import cn.wen.yaling.xo.service.TagService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;



/**
 * 
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class TagController {
    @Autowired
    private TagService tagService;

    @Autowired
    private ArticleService articleService;


    /**
     * 获取全部的标签及标签中的位置数量
     * @return
     */
    @GetMapping("/web/tag/list")
    public ResultEntity getAllTagList() {
        List<TagVO> tagVOList = tagService.getAllTagList();
        return ResultEntity.ok().put("data", tagVOList);
    }

    /**
     * 获取当前标签下的所有文章列表
     * @return
     */
    @GetMapping("/web/tag/article-list")
    public ResultEntity getTagArticleList(@RequestParam("tagId") Integer tagId) {
        List<ArticleListVO> articleListVOS = tagService.getTagArticleList(tagId);
        return ResultEntity.ok().put("data", articleListVOS);
    }

    /**
     * 当前标签的最热文章
     * @param tagId
     * @return
     */
    @GetMapping("/web/tag/article-hot")
    public ResultEntity getTagHotArticleList(@RequestParam("tagId") Integer tagId) {
        List<ArticleSimpleVO> articleListVOS = articleService.getArticleHotListByTag(tagId,  LimitNum.EIGHT.getNum());
        return ResultEntity.ok().put("data", articleListVOS);
    }

    /*========================================== 后台管理 API ===========================================*/

    /**
     * 获取全部的标签包括了禁止使用的标签
     * @return
     */
    @GetMapping("/admin/tag/list")
    public ResultEntity getAllTags() {
        List<TagEntity> tagList = tagService.getTagList();
        return ResultEntity.ok().put("data", tagList);
    }

    /**
     * 模糊查询
     * @return
     */
    @GetMapping("/admin/tag/search")
    public ResultEntity searchTag(@RequestParam("tagName") String tagName) {
        List<TagEntity> tagList = tagService.getTagByTagName(tagName);
        return ResultEntity.ok().put("data", tagList);
    }

    /**
     * 修改标签的状态
     * @return
     */
    @PostMapping("/admin/tag/update-status")
    public ResultEntity updateStatus(@RequestParam("tagId") Integer tagId, @RequestParam("status") Integer status) {
        return tagService.updateTagStatus(tagId, status);

    }

    /**
     * 修改标签
     * @return
     */
    @PostMapping("/admin/tag/update")
    public ResultEntity updateStatus(@RequestBody TagUpdateVO tag) {
        return tagService.updateTag(tag);
    }

    /**
     * 添加标签
     * @return
     */
    @PostMapping("/admin/tag/add")
    public ResultEntity addTag(@RequestBody TagAddVO tag) {
        return tagService.addTag(tag);
    }

    /**
     * 批量删除标签
     * @return
     */
    @PostMapping("/admin/tag/delete")
    public ResultEntity deleteTag(@RequestParam("tagIds") Integer[] tagIds) {
        return tagService.deleteTagByIds(tagIds);
    }



}
